package tdh.common.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tdh.dao.BzdmDao;
import tdh.dao.DepartDao;
import tdh.entity.po.Bzdm;
import tdh.entity.po.Depart;
import tdh.entity.po.User;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 工具类
 * @author 曹昊
 * @date 2021/7/14
 */
@Component
public class Util {
    public static String BMDM = "320100";
    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static HashMap<String,String> yhxbCodeCache;
    private static HashMap<String,String> yhbmCodeCache;
    public static HashMap<String,String> getYhxbCodeCache(){
        if (yhxbCodeCache==null){
            yhxbCodeCache = new HashMap<String, String>();
        }
        return yhxbCodeCache;
    }

    public static void flushYhxbCache(List<Bzdm> bzdms){
        for (Bzdm bzdm : bzdms) {
            System.out.println(bzdm.getCode()+"----->"+bzdm.getMc());
            getYhxbCodeCache().put(bzdm.getCode(),bzdm.getMc());
        }
    }

    public static HashMap<String,String> getYhbmCodeCache(){
        if (yhbmCodeCache==null){
            yhbmCodeCache = new HashMap<String, String>();
        }
        return yhbmCodeCache;
    }

    public static void flushYhbmCache(List<Depart> departs){
        for (Depart depart : departs) {
            getYhbmCodeCache().put(depart.getBmdm(),depart.getBmmc());
        }
    }

    public static String jsonTsf(String userJson){
        userJson = userJson.replace("\\", "");
        userJson = userJson.substring(1, userJson.lastIndexOf("\""));
        return userJson;
    }

    /**
     * @Author caohao
     * @Description 获取当前系统时间
     * @Date 10:30 2021/7/14
     * @Param []
     * @return java.lang.String
     **/
    public static String nowTime(){
        long timeMillis = System.currentTimeMillis();
        Date date = new Date(timeMillis);
        String time = dateFormat.format(date);
        return time;
    }



    @Autowired
    BzdmDao bzdmDao;
    @Autowired
    DepartDao departDao;




    public String getBmmcByBmdm(String bmdm){
        if (getYhbmCodeCache().containsKey(bmdm)) {
            return getYhbmCodeCache().get(bmdm);
        }
        List<Depart> departs = departDao.allDeaprts();
        flushYhbmCache(departs);
        if (getYhbmCodeCache().containsKey(bmdm)) {
            return getYhbmCodeCache().get(bmdm);
        }
        return null;
    }

    public  String getYhxbByXbCode(String xbcode){
        if (getYhxbCodeCache().containsKey(xbcode)) {
            return getYhxbCodeCache().get(xbcode);
        }
        List<Bzdm> bzdms = bzdmDao.bzdms();
        flushYhxbCache(bzdms);
        if (getYhxbCodeCache().containsKey(xbcode)) {
            return getYhxbCodeCache().get(xbcode);
        }
        return null;
    }


    
}
